import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_cupertino_date_picker_fork/flutter_cupertino_date_picker_fork.dart';
import 'package:get/get.dart';

import 'logic.dart';

class AddArchivesPage extends StatelessWidget {
  const AddArchivesPage({super.key});

  @override
  Widget build(BuildContext context) {
    final logic = Get.put(AddArchivesLogic());

    return Scaffold(
      appBar: AppBar(title: const Text("添加档案")),
      body: Column(children: [
        ListTile(
          title: const Text("日期:"),
          trailing: Obx(() {
            return Text(
              "${logic.selectDate} ➡",
              style: logic.listTileFontSize,
            );
          }),
          onTap: () {
            DatePicker.showDatePicker(
              context,
              initialDateTime: DateTime.now(),
              // 初始化日期
              minDateTime: DateTime(2023, 10, 1),
              dateFormat: "yyyy-MM-d",
              locale: DATETIME_PICKER_LOCALE_DEFAULT,
              pickerMode: DateTimePickerMode.date,
              pickerTheme: DateTimePickerTheme.Default,
              onConfirm: (now, _) {
                logic.selectDate.value =
                    "${now.year}-${now.month.toString()}-${now.day}";
              },
              onClose: () {},
            );
          },
        ),
        ListTile(
          title: const Text("地点:"),
          trailing: SizedBox(
            width: 200,
            child: Obx(() {
              return EditableText(
                readOnly: logic.mapIseditOk.value,
                textAlign: TextAlign.end,
                style: TextStyle(
                  fontSize: 16,
                  color: logic.mapIseditOk.value ? Colors.black : Colors.orange,
                ),
                controller: logic.mapCtrl,
                focusNode: FocusNode(),
                cursorColor: Colors.indigo,
                backgroundCursorColor: Colors.cyanAccent,
              );
            }),
          ),
          // 长按之后不可触发单击事件
          onTap: () => logic.locationSelect(context),
          onLongPress: logic.locationSelectLong,
        ),
        ListTile(
          title: const Text(
            "鸡蛋价格（元/斤|箱）X 数量（斤|箱）= 总（元）*",
            style: TextStyle(fontSize: 12),
          ),
          subtitle: Row(
            children: [
              Expanded(
                flex: 1,
                child: Container(
                  // padding: EdgeInsets.fromLTRB(5, 0, 5, 0),
                  margin: const EdgeInsets.fromLTRB(5, 0, 5, 0),
                  height: 50,
                  // width: 120,
                  child: TextField(
                    keyboardType: TextInputType.number,
                    onChanged: logic.onChanged,
                    decoration: logic.dec,
                    controller: logic.pricectrl,
                    focusNode: FocusNode(),
                    style: const TextStyle(fontSize: 20, color: Colors.blue),
                    cursorColor: Colors.lightBlue,
                  ),
                ),
              ),
              const Text("X"),
              Expanded(
                flex: 1,
                child: Container(
                  margin: const EdgeInsets.fromLTRB(5, 0, 5, 0),
                  height: 50,
                  // width: 120,
                  child: TextField(
                    keyboardType: TextInputType.number,
                    onChanged: logic.onChanged,
                    decoration: logic.dec,
                    controller: logic.numberctrl,
                    focusNode: FocusNode(),
                    style: const TextStyle(fontSize: 20, color: Colors.blue),
                    cursorColor: Colors.lightBlue,
                  ),
                ),
              ),
              const Text("="),
              Expanded(
                flex: 2,
                child: Container(
                  margin: const EdgeInsets.fromLTRB(5, 0, 5, 0),
                  height: 60,
                  // width: 120,
                  child: TextField(
                    keyboardType: TextInputType.number,
                    textAlign: TextAlign.center,
                    decoration: logic.dec,
                    controller: logic.rmbctrl,
                    focusNode: FocusNode(),
                    style: const TextStyle(fontSize: 22, color: Colors.blue),
                    cursorColor: Colors.lightBlue,
                  ),
                ),
              ),
            ],
          ),
        ),
        Obx(() {
          return ListTile(
            title: const Text("付钱否？:"),
            trailing: CupertinoSegmentedControl(
              children: {
                0: Text("已结帐", style: logic.listTileFontSize),
                1: Text("未结帐", style: logic.listTileFontSize),
              },
              onValueChanged: logic.onValueChanged,
              groupValue: logic.isSelectedValue.value,
            ),
          );
        }),
        Center(
          child: TextButton(
            onPressed: logic.save,
            style: TextButton.styleFrom(
                backgroundColor: Colors.green,
                minimumSize: const Size(300, 60),
                shadowColor: Colors.lightBlueAccent),
            child: const Text(
              "保存",
              style: TextStyle(fontSize: 32, color: Colors.white),
            ),
          ),
        ),
      ]),
    );
  }
}
